import Vue from "vue";

export const state = {
  //webView地址
  webViewUrl: "",
  loadingShow: false,
  //微信场景参数
  chatScenesInfo: {},
  //登录弹窗状态
  loginPopupShow: false,
  //当前位置
  currentAddress: {
    areaName: "请选择",
    areaId: ''
  },
  // 底部导航栏index
  tabbarindex: 0,
  // 农业类型id
  farmingTypeNo: '',
  farmingTypeName: '',
	hide: false, // 弹窗隐藏显示
	rogress: '0', // 进度\
};
//缓存浏览器的数据名称
const cacheNameList = ["userInfo", "webViewUrl"];
let clearTime;
export const mutations = {
  //取出缓存数据（打开APP就取出）
  setCacheData(state) {
    for (let name of cacheNameList) {
      let data;
      // #ifndef H5
      data = sessionStorage.getItem(name);
      // #endif
      // #ifdef H5
      data = sessionStorage.getItem(name) || localStorage.getItem(name);
      // #endif
      if (data) {
        // #ifdef H5
        try {
          data = JSON.parse(data);
        } catch (e) {}
        // #endif
        state[name] = data;
      }
    }
  },
  //WebView地址
  setWebViewUrl(state, data) {
    if (data) {
      state.webViewUrl = data;
      // #ifdef H5
      window.sessionStorage.setItem('webViewUrl', data);
      // #endif
    }
  },
  //数据加载状态
  setLoadingShow(state, data) {
    if (state.loadingShow) {
      clearTime && clearTimeout(clearTime);
      clearTime = setTimeout(function() {
        state.loadingShow = data;
      }, 300);
    } else {
      state.loadingShow = data;
    }
  },
  //微信场景参数
  setChatScenesInfo(state, data) {
    if (data) {
      state.chatScenesInfo = data;
    }
  },
  //登录弹窗状态
  setLoginPopupShow(state, data) {
    state.loginPopupShow = data;
  },
  //当前地址
  setCurrentAddress(state, data) {
    if (data) {
      state.currentAddress = Object.assign(state.currentAddress, data);
      let addressInfo = {
        "provinceId": state.currentAddress.provinceId,
        "provinceName": state.currentAddress.provinceName,
        "cityId": state.currentAddress.cityId,
        "cityName": state.currentAddress.cityName,
        "areaId": state.currentAddress.areaId,
        "areaName": state.currentAddress.areaName,
      };
      uni.setStorageSync('currentAddress', addressInfo);
    }
  },
  //当前选中的tabbarindex
  setTabbarIndex(state, data) {
    state.tabbarindex = data.tabindex
  },
  // 设置当前选择农场信息
  setFarmingTypeNo(state, data) {
    state.farmingTypeName = data.farmingTypeName
    state.farmingTypeNo = data.farmingTypeNo
  },
  // 重置所有数据
  refreshdata(state) {
    state.farmingTypeName = ""
    state.farmingTypeNo = ""
  },
	// 设置显示隐藏进度弹窗
	setHide(state, val) {
		state.hide = val
	},
	// 设置进度数据
	setRogress(state, val) {
		state.rogress = val
	},
};
export const actions = {

};
